package com.jvm.jdbc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * <p>
 *
 * </p>
 *
 * @author qingshan
 * @since 2020-10-11 15:30
 */
public class ConnectUtil {

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("org.h2.Driver");
            conn = DriverManager.getConnection("jdbc:h2:~/Desktop/note/h2/test", "sa", "sa");
            Statement statement = conn.createStatement();
            // 3、执行操作
            // 3.1、先删除表，若存在
            statement.execute("drop table user if exists ");

            // 3.2、创建表
            statement.execute("create table user(id int primary key, user_name varchar(10), age int , sex varchar(2) )");

            // 4、新增
            statement.executeUpdate("insert into user(id,user_name,age,sex) values(1,'张三',23,'男' )");
            statement.executeUpdate("insert into user(id,user_name,age,sex) values(2,'李四',25,'男' )");
            statement.executeUpdate("insert into user(id,user_name,age,sex) values(3,'王五',33,'男' )");
            statement.executeUpdate("insert into user(id,user_name,age,sex) values(4,'珠帘',23,'女' )");
            statement.executeUpdate("insert into user(id,user_name,age,sex) values(5,'鲤鱼',20,'女' )");
            statement.close();
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        // add application code here
//        conn.close();
        return conn;
    }
}
